<template>
  <el-dialog title="选择科室" width="400px"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :visible.sync="currentVisible"
             :before-close="handleClose" >
    <el-tree :data="data" :props="defaultProps" @node-click="handleClick"></el-tree>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handleSelect">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  name: 'DeptRef',
  components: { },
  props: {
    visible: {
      type: Boolean
    },
    hospitalId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      data: [],
      currentVisible: false,
      selectNode: {}
    }
  },
  methods: {
    ...mapActions(['GetDeptTree']),
    handleClose(done) {
      this.$emit('on-close')
      this.selectNode = {}
      done()
    },
    close() {
      this.$emit('on-close')
      this.selectNode = {}
      this.currentVisible = false
    },
    handleClick(data) {
      this.selectNode = data
    },
    search() {
      this.GetDeptTree(this.hospitalId).then(res => {
        if (res) {
          this.data = res
        }
      }).catch(() => {
      })
    },
    handleSelect() {
      if (this.selectNode && this.selectNode.id) {
        this.$emit('on-select', this.selectNode)
        this.currentVisible = false
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.currentVisible = val
        this.search()
      }
    }
  }
}
</script>
